//
//  OnlineTopHeadView.m
//  GC_OC
//
//  Created by lbh on 2022/4/6.
//

#import "OnlineTopHeadView.h"

@implementation OnlineTopHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
#pragma mark --Priavte
- (void)makeUI{
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.leftButton];
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid(20));
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.height.mas_equalTo(32);
    }];
    
    [self.leftButton addSubview:self.leftLabel];
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(self.leftButton.mas_centerY);
    }];
    
    [self.leftButton addSubview:self.leftImageView];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftLabel.mas_right).offset(3);
        make.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(18, 18));
        make.centerY.mas_equalTo(self.leftButton.mas_centerY);
    }];
    
    [self.bgView addSubview:self.searchBgView];
    [self.searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftButton.mas_right).offset(15);
        make.centerY.mas_equalTo(self.leftButton.mas_centerY);
        make.right.mas_equalTo(-wid(55));
        make.bottom.mas_equalTo(-12);
        make.top.mas_equalTo(8);
    }];
    
    [self.bgView addSubview:self.searchLabel];
    [self.searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.searchBgView);
    }];
    
//    [self.bgView addSubview:self.lineView];
//    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.height.mas_equalTo(1);
//        make.bottom.mas_equalTo(-1);
//    }];
}

- (void)setSearchArray:(NSArray *)searchArray{
    _searchArray = searchArray;
    NSDictionary *classInfo = [FKCacheTool classInfo];
    if (classInfo != nil) {
        NSString *name = classInfo[@"name"];
        self.leftLabel.text = name;
    } else {
        if(self.searchArray.count > 0){
            self.leftLabel.text = self.searchArray.firstObject[@"name"];
        }
    }
}

#pragma mark --Action
- (void)leftButtonAction:(UIButton *)btn{
    NSArray *classArray = self.searchArray;
    NSMutableArray *titleS = [[NSMutableArray alloc]init];
    for (NSDictionary *dict in classArray) {
        NSString *name = dict[@"name"];
        [titleS addObject:name];
    }
    [YBPopupMenu showRelyOnView:btn titles:titleS icons:nil menuWidth:95 otherSettings:^(YBPopupMenu *popupMenu) {
        popupMenu.selectText = self.leftLabel.text;
        popupMenu.textSelectColor = UIColor.blackColor;
        popupMenu.showMaskView = YES;
        popupMenu.isShowSeparator = NO;
        popupMenu.delegate = self;
        popupMenu.isShowShadow = YES;
        popupMenu.menuShawColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        popupMenu.animationManager.style = 0;
        popupMenu.animationManager.duration = 0.01;
        popupMenu.maxVisibleCount = 10;
        popupMenu.minSpace = 0;
        popupMenu.arrowWidth =0;
        popupMenu.rectCorner = UIRectCornerBottomRight;
        popupMenu.cellTextAlignment = NSTextAlignmentLeft;
    }];
}
- (void)searchTap{
    SearchVC *vc = [[SearchVC alloc]init];
    vc.hidesBottomBarWhenPushed=YES;
    vc.searchID = self.searchID;
    [[KFTools getCurrentVC].navigationController pushViewController:vc animated:NO];
}
#pragma mark --YBPopupMenuDelegate
- (void)ybPopupMenuDidDismiss:(YBPopupMenu *)ybPopupMenu{
}
- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index{
    NSDictionary *dict= self.searchArray[index];
    NSString *ID = dict[@"id"];
    NSString *name = dict[@"name"];
    [FKCacheTool saveClassInfo:dict]; // 缓存选中的班级信息
    self.leftLabel.text = name;
    if (self.popMenuDidBlock) {
        self.popMenuDidBlock(ID);
    }
}
#pragma mark --Lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}
- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc]init];
        _leftLabel.font = [UIFont boldSystemFontOfSize:14];
        _leftLabel.textColor = UIColor.whiteColor;
        _leftLabel.text = @"未知";
    }
    return _leftLabel;
}

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]init];
        _leftImageView.image = [UIImage imageNamed:@"upj_white"];
    }
    return _leftImageView;
}
- (UIView *)searchBgView{
    if (!_searchBgView) {
        _searchBgView = [[UIView alloc]init];
        _searchBgView.backgroundColor = UIColor.whiteColor;
        _searchBgView.layer.masksToBounds = YES;
        _searchBgView.layer.cornerRadius = 18;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchTap)];
        [_searchBgView addGestureRecognizer:tap];
    }
    return _searchBgView;
}

- (UILabel *)searchLabel{
    if (!_searchLabel) {
        _searchLabel = [[UILabel alloc]init];
        _searchLabel.font = [UIFont systemFontOfSize:14];//[UIFont fontWithName:@"PingFangSC-Semibold" size:14]
        _searchLabel.textColor = RBG(100,100,100);
        _searchLabel.text = @"输入标题或关键字";
    }
    return _searchLabel;
}
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = RBG(244,244,244);
        _lineView.layer.shadowColor = [UIColor grayColor].CGColor;
        _lineView.layer.shadowOffset = CGSizeMake(0, 0);
        _lineView.layer.shadowOpacity = 0.25;
    }
    return _lineView;
}
@end
